﻿/*******************************************************************
 * 作者：JOHN
 * 类型：添加页面
 * 时间：2011-11-25
 * 说明：
 * ****************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class HosPitalGoods : BasePage
    {
        public HosPitalGoods()
        {
            this.ModuleNo = Module.医院药品分类统计.ToString();
            this.PageOperateType = OperateType.医院药品类别统计.ToString();
        }
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            #region 查询方法

            int t = 0;

            StringBuilder sb = new StringBuilder();

            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

            #region 通用名

            if (cbProductNameLike.Checked == true)
            {
                if (!string.IsNullOrEmpty(txtProductName.Text))
                {
                    if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text) == true)
                    {
                        sb.AppendFormat(" AND ProductName = '{0}'", this.txtProductName.Text);
                    }
                    else
                    {
                        sb.AppendFormat("AND PinyinCode= '{0}'", this.txtProductName.Text);
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(txtProductName.Text))
                {
                    if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text) == true)
                    {
                        sb.AppendFormat(" AND ProductName LIKE '%{0}%'", this.txtProductName.Text);
                    }
                    else
                    {
                        sb.AppendFormat(" AND PinyinCode LIKE '%{0}%'", this.txtProductName.Text);
                    }
                }
            }
            #endregion

            #region CheckBoxList集合

            string Level = "";
            string IsCountry = "";
            //医院等级
            for (int ii = 0; ii < cblboxlist.Items.Count; ii++)
            {
                if (cblboxlist.Items[ii].Selected == true)
                {
                    Level += "'" + cblboxlist.Items[ii].Value + "',";
                }
            }


            if (!string.IsNullOrEmpty(Level))
            {
                sb.AppendFormat(" AND Levels IN ({0})", Level.Substring(0, Level.Length - 1));
            }

            //基层医疗机构
            for (int j = 0; j < chkIsCountry.Items.Count; j++)
            {
                if (chkIsCountry.Items[j].Selected == true)
                {
                    IsCountry += "'" + chkIsCountry.Items[j].Value + "',";
                }
            }

            if (!string.IsNullOrEmpty(IsCountry))
            {
                sb.AppendFormat(" AND IsCountry IN ({0})", IsCountry.Substring(0, IsCountry.Length - 1));
            }

            #endregion


            string timeFrom = string.Intern(this.txtTimeFrom.Text);

            string timeTo = string.Intern(this.txtTimeTo.Text);

            if (!string.IsNullOrEmpty(timeFrom))
            {
                sb.AppendFormat(" AND CenSubmiitTime >= '{0}'", timeFrom);
            }

            if (!string.IsNullOrEmpty(timeTo))
            {
                sb.AppendFormat(" AND CenSubmiitTime <= '{0}'", timeTo);
            }

            if (!string.IsNullOrEmpty(this.txtMedicineModel.Text))
            {
                sb.AppendFormat(" AND MedicineModelName LIKE '%{0}%'", txtMedicineModel.Text);
            }
            if (!string.IsNullOrEmpty(this.txtPack.Text))
            {
                sb.AppendFormat(" AND Pack LIKE '%{0}%'", txtPack.Text);
            }
            if (!string.IsNullOrEmpty(this.txtUnit.Text))
            {
                sb.AppendFormat(" AND Unit LIKE '%{0}%'", txtUnit.Text);
            }
            if (!string.IsNullOrEmpty(this.txtHospitalName.Text))
            {
                sb.AppendFormat(" AND HospitalName LIKE '%{0}%'", txtHospitalName.Text);
            }


            #endregion

            System.Data.DataTable dt = i.GetHospitalGoods(5, sb.ToString(), orderby, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            System.Data.DataTable dtSum = i.GetHospitalGoods(5, sb.ToString(), orderby, 1, 0, out t);
            AspNetPager1.RecordCount = t;
            gvwInfo.PageSize = AspNetPager1.PageSize;
            this.gvwInfo.DataSource = dt;
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            HZ.Supervise.Web.Function.ChangeRate_Sell(dt, "SellRate");
            this.gvwInfo.DataBind();

            double PurchaseAmountTotal = 0;
            double WareHouseAmountTotal = 0;
            double SellAmountTotal = 0;
            for (int j = 0; j < dtSum.Rows.Count; j++)
            {
                PurchaseAmountTotal += Convert.ToDouble(dtSum.Rows[j][9]);
                WareHouseAmountTotal += Convert.ToDouble(dtSum.Rows[j][8]);
                SellAmountTotal += Convert.ToDouble(dtSum.Rows[j][13]);
            }

            this.lblCount.Text = "采购总金额为：" + PurchaseAmountTotal.ToString("0.000") + ";" + "入库总金额：" + WareHouseAmountTotal.ToString("0.000") + ";" + "销售总金额：" + SellAmountTotal.ToString("0.000") + ";";

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            #region 查询方法

            int t = 0;

            StringBuilder sb = new StringBuilder();

            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

            #region 通用名

            if (cbProductNameLike.Checked == true)
            {
                if (!string.IsNullOrEmpty(txtProductName.Text))
                {
                    if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text) == true)
                    {
                        sb.AppendFormat(" AND ProductName = '{0}'", this.txtProductName.Text);
                    }
                    else
                    {
                        sb.AppendFormat("AND PinyinCode= '{0}'", this.txtProductName.Text);
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(txtProductName.Text))
                {
                    if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text) == true)
                    {
                        sb.AppendFormat(" AND ProductName LIKE '%{0}%'", this.txtProductName.Text);
                    }
                    else
                    {
                        sb.AppendFormat(" AND PinyinCode LIKE '%{0}%'", this.txtProductName.Text);
                    }
                }
            }
            #endregion


            #region CheckBoxList集合

            string Level = "";
            string IsCountry = "";
            //医院等级
            for (int ii = 0; ii < cblboxlist.Items.Count; ii++)
            {
                if (cblboxlist.Items[ii].Selected == true)
                {
                    Level += "'" + cblboxlist.Items[ii].Value + "',";
                }
            }


            if (!string.IsNullOrEmpty(Level))
            {
                sb.AppendFormat(" AND Levels IN ({0})", Level.Substring(0, Level.Length - 1));
            }

            //基层医疗机构
            for (int j = 0; j < chkIsCountry.Items.Count; j++)
            {
                if (chkIsCountry.Items[j].Selected == true)
                {
                    IsCountry += "'" + chkIsCountry.Items[j].Value + "',";
                }
            }

            if (!string.IsNullOrEmpty(IsCountry))
            {
                sb.AppendFormat(" AND IsCountry IN ({0})", IsCountry.Substring(0, IsCountry.Length - 1));
            }
            #endregion


            string timeFrom = string.Intern(this.txtTimeFrom.Text);

            string timeTo = string.Intern(this.txtTimeTo.Text);

            if (!string.IsNullOrEmpty(timeFrom))
            {
                sb.AppendFormat(" AND CenSubmiitTime >= '{0}'", timeFrom);
            }

            if (!string.IsNullOrEmpty(timeTo))
            {
                sb.AppendFormat(" AND CenSubmiitTime <= '{0}'", timeTo);
            }

            if (!string.IsNullOrEmpty(this.txtMedicineModel.Text))
            {
                sb.AppendFormat(" AND MedicineModelName LIKE '%{0}%'", txtMedicineModel.Text);
            }
            if (!string.IsNullOrEmpty(this.txtPack.Text))
            {
                sb.AppendFormat(" AND Pack LIKE '%{0}%'", txtPack.Text);
            }
            if (!string.IsNullOrEmpty(this.txtUnit.Text))
            {
                sb.AppendFormat(" AND Unit LIKE '%{0}%'", txtUnit.Text);
            }
            if (!string.IsNullOrEmpty(this.txtHospitalName.Text))
            {
                sb.AppendFormat(" AND HospitalName LIKE '%{0}%'", txtHospitalName.Text);
            }

            #endregion

            System.Data.DataTable dt = i.GetHospitalGoods(5, sb.ToString(), orderby, 1, 0, out t);

            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            HZ.Supervise.Web.Function.ChangeRate_Sell(dt, "SellRate");

            if (dt.Rows.Count <= 0)
            {
                HZ.Web.WebUtil.Alert("没有要导出的数据！", this);
            }
            else
            {
                HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwInfo);
            }
        }
    }
}
